package com.xu.wemall.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.xu.wemall.easyexcel.ExcelEventListener;
import com.xu.wemall.easyexcel.TableDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Api(tags = "Excel操作接口")
@RestController
@RequestMapping("/excel")
public class ExcelController {

    @ApiOperation("读取合并单元格")
    @PostMapping(value = "/extraReadFile")
    public void extraReadFile(@RequestPart("file") MultipartFile file) throws IOException {

        EasyExcel.read(file.getInputStream(), TableDto.class, new ExcelEventListener<>())
                // 需要读取批注 默认不读取
                .extraRead(CellExtraTypeEnum.COMMENT)
                // 需要读取超链接 默认不读取
                .extraRead(CellExtraTypeEnum.HYPERLINK)
                // 需要读取合并单元格信息 默认不读取
                .extraRead(CellExtraTypeEnum.MERGE)
                .sheet()
                .headRowNumber(1)
                .doRead();
    }

    @ApiOperation("读取合并单元格")
    @PostMapping("/importPermissionTableV1")
    public void importPermissionTableV1(@RequestPart("file") MultipartFile file) {

        List<TableDto> permissionList=new ArrayList<>();
        try {

            ExcelEventListener listener = new ExcelEventListener();
            EasyExcel.read(file.getInputStream(), TableDto.class, listener)
                    .extraRead(CellExtraTypeEnum.MERGE)
                    .sheet()
                    .headRowNumber(1)
                    .doRead();
            permissionList = listener.getList();
            System.out.println("===============================");
            permissionList.forEach(System.out::println);
            //todo check-->do--->save--->result
        } catch (IOException e) {
            log.info(e.getMessage());
            throw new IllegalArgumentException(e.getMessage());
        }
    }

    @Operation(summary = "导入Excel")
    @PostMapping(value = "/importExcel", consumes = "multipart/form-data")
    @ResponseBody
    @Transactional
    public void importExcel(@RequestPart("file") MultipartFile file) throws IOException {
        try {
            InputStream inputStream = file.getInputStream();
            //创建两个AnalysisEventListener
            ExcelEventListener<TableDto> listener1 = new ExcelEventListener<>();
            ExcelEventListener<TableDto> listener2 = new ExcelEventListener<>();

            //读取文件
            //创建ExcelReaderBuilder实例
            ExcelReaderBuilder readerBuilder = EasyExcel.read();
            //可以传入流作为参数
            readerBuilder = EasyExcel.read(inputStream);
            //读取合并单元格
            readerBuilder.extraRead(CellExtraTypeEnum.MERGE);
            //读取评论
            readerBuilder.extraRead(CellExtraTypeEnum.COMMENT);
            //读取超文本链接
            readerBuilder.extraRead(CellExtraTypeEnum.HYPERLINK);
            //获取文件对象
            readerBuilder.file("D:\\java\\用户数据表.xls");
            //指定sheet
            readerBuilder.sheet("用户数据表");
            //自动关闭输入流
            readerBuilder.autoCloseStream(true);
            //设置Excel文件格式
            readerBuilder.excelType(ExcelTypeEnum.XLS);
            //注册监听器进行数据的解析
            readerBuilder.registerReadListener(listener1);
            //构建读取器
            ExcelReader reader = readerBuilder.build();

            //读取多Sheet，多Sheet必须一次性传入，否则中间文件流会中断
            ReadSheet readSheet1 = EasyExcel.readSheet(0)
                    .head(TableDto.class)
                    .registerReadListener(listener1)
                    .headRowNumber(1)             //从第1行而不是从第0行开始读
                    .build();
            ReadSheet readSheet2 = EasyExcel.readSheet(1)
                    .head(TableDto.class)
                    .registerReadListener(listener2)
                    .headRowNumber(1)//从第1行而不是从第0行开始读
                    .build();
            reader.read(readSheet1, readSheet2);

            //读取数据
            reader.readAll();
            //读取完毕
            reader.finish();
            //关闭流
            inputStream.close();

        } catch (IOException e) {
            log.info(e.getMessage());

        }
    }
}
